package cn.edu.baiyunu.myapplication.ui.screens.home

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import cn.edu.baiyunu.myapplication.ui.components.GradientBackground
import cn.edu.baiyunu.myapplication.ui.screens.home.components.BalanceCard
import cn.edu.baiyunu.myapplication.ui.screens.home.components.HomeAppBar
import cn.edu.baiyunu.myapplication.ui.screens.home.components.QuickActionGrid
import cn.edu.baiyunu.myapplication.ui.screens.home.components.RecentTransactionsList
import cn.edu.baiyunu.myapplication.ui.screens.home.viewmodel.HomeViewModel

/**
 * 首页屏幕，新设计基于原型图实现
 */
@Composable
fun HomeScreen(
    navigateToTransaction: (String) -> Unit = {},
    navigateToAddTransaction: () -> Unit = {},
    navigateToAllTransactions: () -> Unit = {},
    navigateToStatistics: () -> Unit = {},
    navigateToProfile: () -> Unit = {},
    navigateToBudget: () -> Unit = {},
    navigateToSettings: () -> Unit = {},
    viewModel: HomeViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsState()
    val scrollState = rememberScrollState()
    
    // 创建渐变背景
    GradientBackground {
        Box(
            modifier = Modifier.fillMaxSize()
        ) {
            // 显示加载状态
            if (uiState.isLoading) {
                CircularProgressIndicator(
                    modifier = Modifier.align(Alignment.Center),
                    color = Color.White
                )
            } else if (uiState.error != null) {
                // 显示错误信息
                Text(
                    text = uiState.error ?: "未知错误",
                    color = Color.White,
                    modifier = Modifier.align(Alignment.Center)
                )
            } else {
                // 内容 - 添加状态栏内边距和滚动功能
                Column(
                    modifier = Modifier
                        .fillMaxSize()
                        .statusBarsPadding() // 添加状态栏内边距
                        .padding(horizontal = 20.dp, vertical = 10.dp)
                        .verticalScroll(scrollState) // 添加滚动功能
                ) {
                    // 顶部应用栏
                    HomeAppBar(
                        onProfileClick = navigateToProfile
                    )
                    
                    Spacer(modifier = Modifier.height(25.dp))
                    
                    // 余额卡片
                    BalanceCard(
                        monthlyBalance = uiState.monthlyBalance,
                        monthlyIncome = uiState.monthlyIncome,
                        monthlyExpense = uiState.monthlyExpense
                    )
                    
                    Spacer(modifier = Modifier.height(25.dp))
                    
                    // 快速操作网格
                    QuickActionGrid(
                        onAddTransaction = navigateToAddTransaction,
                        onViewStatistics = navigateToStatistics,
                        onBudget = navigateToBudget,
                        onSettings = navigateToSettings
                    )
                    
                    Spacer(modifier = Modifier.height(25.dp))
                    
                    // 最近交易列表
                    RecentTransactionsList(
                        transactions = uiState.recentTransactions,
                        categoryMap = uiState.categoryMap,
                        onViewAllClick = navigateToAllTransactions,
                        onTransactionClick = navigateToTransaction
                    )
                    
                    // 添加底部空间，避免被导航栏遮挡
                    Spacer(modifier = Modifier.height(100.dp))
                }
            }
        }
    }
} 